import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
import 'package:lcapp/api/interface/home_api.dart';
import 'package:lcapp/api/interface/user_api.dart';
import 'package:lcapp/api/request/response_data.dart';
import 'package:lcapp/basic/user_info.dart';
import 'package:lcapp/config/const_config.dart';
import 'package:lcapp/data/xian_huo_data_entity.dart';
import 'package:lcapp/model/refresh/refresh.dart';
import 'package:lcapp/page/search/xianhuo_item.dart';
import 'package:lcapp/widget/asr_app_bar.dart';

class ChooseXianHuoPage extends StatefulWidget {
  const ChooseXianHuoPage({Key? key}) : super(key: key);

  @override
  _ChooseXianHuoPageState createState() => _ChooseXianHuoPageState();
}

class _ChooseXianHuoPageState extends State<ChooseXianHuoPage> {

  TextEditingController controller = TextEditingController();
  List<XianHuoDataEntity> xianHuos = [];
  XianHuoDataEntity? chooseEntity;
  int pageNum = 1;
  int pageSize = 10;
  bool isBtn = true;

  int fabuType = 0;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getXianHuo();
  }
  getXianHuo()async{
    if(userInfo.id == null){
      return;
    }
    ResponseData res = await UserAPI.getMinXianHuos(userInfo.id!,pageNum,pageSize,keyword: controller.text.trim().isEmpty?null:controller.text.trim(),choose: 1);
    isBtn = true;
    if(res.code==200){
      if(pageNum==1){
        xianHuos.clear();
      }
      List arr = res.data??[];
      for (var element in arr) {
        XianHuoDataEntity entity = XianHuoDataEntity().fromJson(element);
        xianHuos.add(entity);
      }
    }
    if(mounted){setState(() {
    });}
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: bgColor,
      appBar: AsrAppBar(
        titleText: "我的现货",
      ),
      body: Stack(
        children: [
          Container(
            child: Column(
              children: [
                Container(
                  margin: EdgeInsets.symmetric(vertical: 30.w),
                  height: 80.w,
                  width: 1.sw-60.w,
                  padding: EdgeInsets.only(left: 20.w),
                  decoration: BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.circular(80.w)
                  ),
                  child: CupertinoSearchTextField(
                    controller: controller,
                    autocorrect: true,
                    autofocus: false,
                    placeholder: "输入您想要的内容",
                    backgroundColor: Colors.transparent,
                    placeholderStyle: TextStyle(
                      color: const Color(0xFFAEAEAE),
                      fontSize: 26.sp,
                      fontWeight: FontWeight.w500,
                    ),
                    style: TextStyle(
                      color: Colors.black,
                      fontSize: 26.sp,
                      fontWeight: FontWeight.w500,
                    ),
                    onChanged: (value){
                      setState(() {
                        isBtn = false;
                      });
                    },
                    onSubmitted: (value){
                      pageNum = 1;
                      getXianHuo();
                    },
                  ),
                ),
                Expanded(
                    child: Container(
                      padding: EdgeInsets.symmetric(horizontal: 30.w),
                      child: EasyRefresh(
                        header: myRefreshHeader(),
                        footer: myRefreshFooter(),
                        onRefresh: ()async{
                          pageNum = 1;
                          getXianHuo();
                        },
                        onLoad: ()async{
                          pageNum++;
                          getXianHuo();
                        },
                        child: AlignedGridView.count(
                          padding: EdgeInsets.zero,
                          crossAxisCount: 2,
                          itemCount: xianHuos.length,
                          crossAxisSpacing: 10.w,
                          mainAxisSpacing: 10.w,
                          itemBuilder: (context,index){
                            return XianHuoItem(entity: xianHuos[index],chooseFun: (){
                              setState(() {
                                for (var data in xianHuos) {data.isChoose=false;}
                                chooseEntity = xianHuos[index];
                                chooseEntity?.isChoose = true;
                                Navigator.of(context).pop(chooseEntity);
                              });
                            },);
                          },
                        ),
                      ),
                    )
                ),
              ],
            ),
          ),
          Positioned(
              child: Container(
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.end,
                  crossAxisAlignment: CrossAxisAlignment.end,
                  children: [
                    Visibility(child: GestureDetector(
                      onTap: (){
                        Navigator.of(context).pushNamed('FaBuSoldPage',arguments: {
                          'fabuType':0,
                        }).then((value){
                          pageNum=1;
                          getXianHuo();
                        });
                        setState(() {
                          fabuType = 0;
                        });
                      },
                      child: Container(
                        margin: EdgeInsets.only(bottom: 20.w),
                        child: Image.asset('assets/home/xh_fabu_cs.png',width: 200.w,),
                      ),
                    ),visible: fabuType==1,),
                    Visibility(child: GestureDetector(
                      onTap: (){
                        Navigator.of(context).pushNamed('FaBuSoldPage',arguments: {
                          'fabuType':1,
                        }).then((value){
                          pageNum=1;
                          getXianHuo();
                        });
                        setState(() {
                          fabuType = 0;
                        });
                      },
                      child: Container(
                        margin: EdgeInsets.only(bottom: 20.w),
                        child: Image.asset('assets/home/xh_fabu_cg.png',width: 200.w,),
                      ),
                    ),visible: fabuType==1,),
                    Visibility(child: GestureDetector(
                      onTap: (){
                        setState(() {
                          fabuType = 0;
                        });
                      },
                      child: Container(
                        child: Image.asset('assets/home/xh_fabu_qx.png',width: 200.w,),
                      ),
                    ),visible: fabuType==1,),
                    Visibility(child: GestureDetector(
                      onTap: (){
                        setState(() {
                          fabuType = 1;
                        });
                      },
                      child: Container(
                        child: Image.asset('assets/home/xh_fabu_add.png',width: 200.w,),
                      ),
                    ),visible: fabuType==0,),

                  ],
                ),
              ),
            right: 40.w,
            bottom: 40.w,
          )
        ],
      ),
      bottomNavigationBar: Visibility(child: GestureDetector(
        onTap: (){
          Navigator.of(context).pop(chooseEntity);
        },
        child: Container(
          width: 1.sw,
          height: 120.w,
          alignment: Alignment.center,
          decoration: BoxDecoration(
            gradient: const LinearGradient(
                colors: [
                  Color(0xffF1783B),
                  Color(0xffF6A456),
                ],
                begin: Alignment.topCenter,
                end: Alignment.bottomCenter
            ),
          ),
          child: Text('确认',style: TextStyle(
              color: Colors.white,
              fontSize: 34.sp,
              fontWeight: FontWeight.w500
          ),),
        ),
      ),visible: false,),
    );
  }
}
